<?php

class OtherService extends BaseService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }


    private $mArticle               = NULL;
    private $mStorage               = NULL;
    private $mForest                = NULL;
    private $mHarbour               = NULL;
    private $mHarbourNeed           = NULL;
    private $mAds                   = NULL;
    private $mNotice                = NULL;
    private $mHelpCenter            = NULL;
    private $mVideo                 = NULL;


    public function __construct()
    {
        $this->mArticle              = new Involved_SupplierModel();
        $this->mStorage              = new Involved_StorageModel();
        $this->mForest               = new Involved_ForestModel();
        $this->mHarbour              = new Involved_HarbourModel();
        $this->mHarbourNeed          = new Involved_HarbourNeedModel();
        $this->mAds                  = new Involved_AdsModel();
        $this->mAds                  = new Involved_AdsModel();
        $this->mNotice               = new Involved_NoticeModel();
        $this->mHelpCenter           = new Involved_HelpCenterModel();
        $this->mVideo                = new Involved_VideoModel();
    }


    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author  alice
     * @date    2016-1-16
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if(empty($arr)) return true;
        $obj    =   (array) $obj;

        foreach($arr as $v){
            if(empty($obj[$v])){
                return false;}
        }
        return true;
    }

    /**
     * 仓储地展示
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowStorageList()
    {
        return $this->mStorage->ShowStorage();
    }

    /**
     * 仓储地详情
     * @param $id
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowStorageInfo($id)
    {
        return $this->mStorage->ShowStorageInfo($id);
    }

    /**
     * 新增仓储地
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddStorage($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['storage_name',]);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        if(!empty($paramObj->storage_img))
        {
            #上传图片处理
            $file = $paramObj->storage_img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['storage_img'] = '/resource/upload/' . $newName;
        }


        $aData = [
        'storage_name'              =>     $paramObj->storage_name,
        'storage_address'           =>     $paramObj->storage_address,
        'quote'                     =>     $paramObj->quote,
        'other_fee'                 =>     $paramObj->other_fee,
        'introduce'                 =>     $paramObj->introduce,
    ];

        $storage_id =  $this->mStorage->insert($aData);

        $video_url = QiniuUpload::instance()->upload_files($paramObj->video_url);

        $video = [
            'storage_id'  =>  $storage_id,
            'video_url'  =>  $video_url,
        ];

        return $this->mVideo->insert($video);




    }

    /**
     * 修改仓储地
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     */
    public function EditStorage($paramObj)
    {

        #上传图片处理
        if(!empty($paramObj->storage_img)){

            $file = $paramObj->storage_img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['storage_img'] = '/resource/upload/' . $newName;
        }

        $aData = [
            'storage_name'              =>     $paramObj->storage_name,
            'storage_address'           =>     $paramObj->storage_address,
            'quote'                     =>     $paramObj->quote,
            'other_cost'                =>     $paramObj->other_cost,
            'introduce'                 =>     $paramObj->introduce,

        ];

        return $this->mStorage->update($aData,['storage_id'=>$paramObj->storage_id]);

    }

    /**
     * 林场列表
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowForestList()
    {
        return $this->mForest->ShowForestList();
    }

    /** 林场详情
     * @param $id
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowForestInfo($id)
    {
        return $this->mForest->ShowForestInfo($id);
    }

    /**
     * 新增林场
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddForest($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['forest_name',]);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        if(!empty($paramObj->img))
        {
            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;
        }

        $aData = [
            'forest_name'               =>     $paramObj->forest_name,
            'country'                   =>     $paramObj->country,
            'product_cate'              =>     $paramObj->product_cate,
            'introduce'                 =>     $paramObj->introduces,
        ];

        $forest_id = $this->mForest->insert($aData);

            $video_url = QiniuUpload::instance()->upload_files($paramObj->video_url);

            $video = [
                'forest_id'  =>  $forest_id,
                'video_url'  =>  $video_url,
            ];

            return $this->mVideo->insert($video);
    }

    /**
     * 修改林场
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     */
    public function EditForest($paramObj)
    {
        if(!empty($paramObj->img)){

            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;

        }

        $aData = [
            'forest_name'               =>     $paramObj->forest_name,
            'country'                   =>     $paramObj->country,
            'product_cate'              =>     $paramObj->product_cate,
            'introduces'                =>     $paramObj->introduces,
        ];


        return $this->mForest->update($aData,['forest_id'=>$paramObj->forest_id]);
    }

    /**
     * 港口展示
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowHarbourList()
    {
        return $this->mHarbour->ShowHarbourList();
    }

    /**
     * 港口详情
     * @param $id
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowHarbourInfo($id)
    {
        return $this->mHarbour->ShowHarbourInfo($id);
    }

    /**
     * 新增港口
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddHarbour($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['harbour_name']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        if(!empty($paramObj->img))
        {
            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;
        }

        $aData = [
            'harbour_name'              =>     $paramObj->harbour_name,
            'city'                      =>     $paramObj->city,
            'quote'                     =>     $paramObj->quote,
            'introduce'                 =>     $paramObj->introduces,
            'other_cost'                =>     $paramObj->other_cost,

        ];

        $harbour_id = $this->mHarbour->insert($aData);

        $video_url = QiniuUpload::instance()->upload_files($paramObj->video_url);

        $video = [
            'harbour_id'  =>  $harbour_id,
            'video_url'  =>  $video_url,
        ];

        return $this->mVideo->insert($video);

    }

    /**
     * 修改港口
     * @param $paramObj
     * @author  alice
     * @date    2016-1-19
     * @return bool
     * @throws Exception
     */
    public function EditHarbour($paramObj)
    {
        if(!empty($paramObj->img))
        {
            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;
        }

        $aData = [
            'harbour_name'              =>     $paramObj->harbour_name,
            'city'                      =>     $paramObj->city,
            'quote'                     =>     $paramObj->quote,
            'introduces'                =>     $paramObj->introduces,
            'other_cost'                =>     $paramObj->other_cost,

        ];

        return $this->mHarbour->update($aData,['harbour_id'=>$paramObj->harbour_id]);
    }

    /**
     * 港口需求展示
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowHarbourNeedList()
    {
        return $this->mHarbourNeed->ShowHarbourNeedList();
    }

    /**
     * 新增港口需求
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     */
    public function AddHarbourNeed($paramObj)
    {

        $aData = [
            'product_name'              =>     $paramObj->product_name,
            'number'                    =>     $paramObj->number,
            'spec'                      =>     $paramObj->spec,
            'expire_time'               =>     $paramObj->expire_time,
            'certificate'               =>     $paramObj->certificate,
            'introduce'                 =>     $paramObj->introduce,
            'contacts'                  =>     $paramObj->contacts,
            'mobile'                    =>     $paramObj->mobile,

        ];

        return $this->mHarbourNeed->insert($aData);
    }

    /**
     * 广告图列表
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowAdsList()
    {
        return $this->mAds->ShowAdsList();
    }

    /**
     * 广告图详情
     * @param $id
     * @author  alice
     * @date    2016-1-19
     * @return mixed
     */
    public function ShowAdsInfo($id)
    {
        return $this->mAds->ShowAdsInfo($id);
    }

    /**
     * 添加广告图
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddAds($paramObj)
    {
        if(!empty($paramObj->img))
        {
            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;

        }

        $aData = [
            'ads_name'      => $paramObj->ads_name,
            'type'          => $paramObj->type,
            'url'           => $paramObj->url,
            'order_by'      => $paramObj->order_by,
            'created'       => time(),
        ];

        return $this->mAds->insert($aData);



    }

    /**
     * 修改广告图
     * @param $paramObj
     * @author  alice
     * @date    2016-1-19
     * @return bool
     */
    public function EditAds($paramObj)
    {
        if(!empty($paramObj->img))
        {
            #上传图片处理
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(375, 275);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;

        }

        $aData = [
            'ads_name'      => $paramObj->ads_name,
            'type'          => $paramObj->type,
            'url'           => $paramObj->url,
            'order_by'      => $paramObj->order_by,
            'created'       => time(),
        ];

        return $this->mAds->update($aData,['ads_id',$paramObj->ads_id]);

    }

    /**
     * 消息展示
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowNoticeList()
    {
        return $this->mNotice->ShowNoticeList();
    }

    /**
     * 消息详情
     * @param $id
     * @author  alice
     * @date    2016-1-18
     * @return mixed
     */
    public function GetNoticeInfo($id)
    {
        return $this->mNotice->ShowNoticeInfo($id);
    }

    /**
     * 添加消息
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddNotice($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['title', 'type', 'content']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        $aData = [
            'title'          => $paramObj->title,
            'type'           => $paramObj->type,
            'content'        => $paramObj->content,
            'created'        => time(),
        ];

        return $this->mNotice->insert($aData);

    }

    /**
     * 修改消息
     * @param $paramObj
     * @author  alice
     * @date    2016-1-18
     * @return bool
     */
    public function EditNotice($paramObj)
    {
        #修改消息
        $aData = [
            'title'          => $paramObj->title,
            'type'           => $paramObj->type,
            'content'        => $paramObj->content,
            'created'        => time(),
        ];

        return $this->mNotice->update($aData,['notice_id'=>$paramObj->notice_id]);

    }

    /**
     * 帮助中心列表
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function ShowHelpList()
    {
        return $this->mHelpCenter->ShowHelpList();
    }

    /**
     * 帮助中心详情
     * @param $id
     * @author  alice
     * @date    2016-1-16
     * @return mixed
     */
    public function GetHelpInfo($id)
    {
        return $this->mHelpCenter->GetHelpInfo($id);
    }
    /**
     * 添加帮助中心
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddHelp($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['name']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        $aData = [
            'name'           => $paramObj->name,
            'content'        => $paramObj->content,
        ];

        return $this->mHelpCenter->insert($aData);

    }

    /**
     * 修改帮助中心
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     */
    public function EditHelp($paramObj)
    {
        $aData = [
            'name'           => $paramObj->name,
            'content'        => $paramObj->content,
        ];

        return $this->mHelpCenter->update($aData,['rec_id' => $paramObj->rec_id]);

    }

    /**
     * 删除帮助中心
     * @param $paramObj
     * @author  alice
     * @date    2016-1-16
     * @return int
     */
    public function DeleteHelp($paramObj)
    {
        return $this->mHelpCenter->delete(['rec_id' => $paramObj->id]);
    }

    /**
     * 视频列表
     * @author  alice
     * @date    2016-1-19
     * @return mixed
     */
    public function ShowVideoList()
    {
        return $this->mVideo->ShowVideoList();
    }

    /**
     * 视频详情
     * @param $id
     * @author  alice
     * @date    2016-1-19
     * @return mixed
     */
    public function ShowVideoInfo($id)
    {
        return $this->mVideo->ShowVideoInfo($id);
    }

    /**
     * 添加视频
     * @param $paramObj
     * @author  alice
     * @date    2016-1-19
     * @return int
     * @throws Exception
     */
    public function AddVideo($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['video_name']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        if(!empty($paramObj->video_url))
        {
            $file = $paramObj->video_url;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/video', $newName);

            $aData['video_url'] = '/resource/video/' . $newName;

        }

        $aData = [
            'video_name'           => $paramObj->video_name,
            'take_time'            => $paramObj->take_time,
        ];

         return $this->mVideo->insert($aData);
    }

    /**
     * 修改视频
     * @param $paramObj
     * @author  alice
     * @date    2016-1-19
     * @return bool
     */
    public function EditVideo($paramObj)
    {
        if(!empty($paramObj->video_url))
        {
            $file = $paramObj->video_url;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/video', $newName);

            $aData['video_url'] = '/resource/video/' . $newName;

        }

        $aData = [
            'video_name'           => $paramObj->video_name,
            'take_time'            => $paramObj->take_time,
        ];

        return $this->mVideo->update($aData,['video_id'=>$paramObj->video_id]);
    }







}